package cn.itcast.web.task;

import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.system.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定时执行的任务类
 */
public class MyTask {
    /**
     * 定时执行的任务方法, 5秒执行一次
     */
    @Reference
    private ContractService contractService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private UserService userService;
    public void execute(){
        /**
         * 需求：交货日期到，邮件提醒
         * SELECT * FROM co_contract WHERE delivery_period='当前时间'
         */
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        List<Contract> timeOut = contractService.findTimeOut();
        if(timeOut.size()>0&&timeOut!=null){
            for (Contract contract : timeOut) {
                User user = userService.findById(contract.getCompanyId());
                Map<String, String> map = new HashMap<>();
                String title = "交货期限即将到期通知";
                String content = "交货期限即将截至，请及时确认船期与货物！如有特殊情况，请及时告知合作方并进行双方的协商沟通。";
                map.put("email",user.getEmail());
                map.put("title", title);
                map.put("content", content);
                rabbitTemplate.convertAndSend("myExchange","msg.email",map);
            }

        }
    }
}
